package com.superdaxue.rxsampledemo.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.superdaxue.rxsampledemo.view.ShowContentView;

/**
 * Created by ZX_CC on 2016/5/25.
 */

public class NetWorkBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ShowContentView showContentView = (ShowContentView) context;
        String action = intent.getAction();
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isAvailable()){//有网络
                    if(networkInfo.getType()==ConnectivityManager.TYPE_WIFI){//WiFi
                        showContentView.onHaveWifi();
                    }else if (networkInfo.getType()==ConnectivityManager.TYPE_MOBILE){//流量

                    }
                }else{//没有网路
                    showContentView.onNoNetWork();
                }
        }
    }
}
